<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$request_link_class = util_link_filter($request_link_class);
$request_link_subclass = util_link_filter($request_link_subclass);
$request_link_detail = util_link_filter($request_link_param['0']);

//
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'app.function.php');

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'frontend.app.class.php');
$feObj = new FrontendAppBase();

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.validate.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'common.validate.class.php');
$vObj = new CommonValidate();

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.upload.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.file.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'attachment.function.php');
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'thumb.function.php');

//
//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.resource.category.class.php');
//include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.resource.class.php');
//$rescObj = new CoreResourceCategoryBase();
//$resObj = new CoreResourceBase();
//include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'attachment.function.php');
//

//call
//common attach
if( $action != '' ) {
	//timestamp
	$timestamptfile = CONFIG_PATH . CONFIG_DIR_CACHE . 'timestamp.txt';
	$fp = fopen($timestamptfile, 'w');
	fwrite($fp, time());
	fclose($fp);
	//
	$attachments_ids = array();
	if( isset($_FILES['attachments_files']) ) {
		$attachmentfileObj = new CoreMultiUpload('attachments_files');
		$attachmentfileObj->setDestination(CONFIG_PATH . CONFIG_DIR_RESOURCES);
		if( $attachmentfileObj->parse() && $attachmentfileObj->save() ) {
			$attachments_savefilenames = $attachmentfileObj->save_filename;
			//$attachments_savefiles = $attachmentfileObj->save_file;
			$attachments_filenames = $attachmentfileObj->filename;
			$attachments_filetypes = $attachmentfileObj->filetype;
			$attachments_filesizes = $attachmentfileObj->filesize;
			//
			if( is_array($attachments_savefilenames) ) {
				for($i=0, $n=sizeof($attachments_savefilenames); $i<$n; $i++) {
					if( $attachments_savefilenames[$i] ) {
						$data_array = array(
									'resources_filename' => $attachments_filenames[$i],
									'resources_filetype' => $attachments_filetypes[$i],
									'resources_filesize' => $attachments_filesizes[$i],
									'resources_value' => $attachments_savefilenames[$i],
									'resources_name' => $attachments_filenames[$i],
									'resources_description' => $_POST['attachments_desc'][$i],
									'resources_status' => '1',
										);
						$resources_id = $resObj->addItem($data_array);
					} else {
						$resources_id = '';
					}
					
					$attachments_ids[] = $resources_id;
				}
			}
		}
		//filter
		if( isset($_POST['field_introtext']) ) {
			$_POST['field_introtext'] = func_attachment_post_convert($_POST['field_introtext'], $attachments_ids);
		}
		if( isset($_POST['field_fulltext']) ) {
			$_POST['field_fulltext'] = func_attachment_post_convert($_POST['field_fulltext'], $attachments_ids);
		}
		if( isset($_POST['field_description']) ) {
			$_POST['field_description'] = func_attachment_post_convert($_POST['field_description'], $attachments_ids);
		}
	}
	//
}

//
if( $request_link_class ) {
	switch($request_link_class) {
	//---------------------------------------------------------------------------//
		case 'call':
			include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/call.app.front.php');
			break;
		case 'callback':
			include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/callback.app.front.php');
			break;
		case 'interface':
			include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/interface.app.front.php');
			break;
		case 'ws':
			include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/ws.app.front.php');
			break;
		case 'xml':
			include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/xml.app.front.php');
			break;
	//---------------------------------------------------------------------------//
		default:
			//if( util_file_exists(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/', $request_link_class, '.app.front.php') ) {
			//	include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/' . $request_link_class . '.app.front.php');
			//} else {
			//	include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/control.app.front.php');
			//}
			//break;
			$request_notexist = false;
			define('APP_KEY', $request_link_class);
			define('APP_URL', 'app/' . $request_link_class . '/');
			define('APP_PATH', CONFIG_PATH . CONFIG_DIR_FRONTS . 'apps/' . $request_link_class . '/');
			if( is_dir(APP_PATH) ) {
				$app_home_file = APP_PATH . 'home.' . $request_link_class . '.app.front.php';
				$app_front_file = APP_PATH . '_front.' . $request_link_class . '.app.front.php';
				$app_detail_file = APP_PATH . $request_link_subclass . '.' . $request_link_class . '.app.front.php';
				
				$app_admin_home_file = APP_PATH . 'admin/' . 'home.admin.' . $request_link_class . '.app.front.php';
				$app_admin_front_file = APP_PATH . 'admin/' . '_front.admin.' . $request_link_class . '.app.front.php';
				$app_admin_detail_file = APP_PATH . 'admin/' . $request_link_detail . '.admin.' . $request_link_class . '.app.front.php';
				//
				if( $request_link_subclass == 'admin' ) {
					if( $request_link_detail != '' && is_file($app_admin_detail_file) ) {
						if( file_exists($app_admin_front_file) ) {
							include_once($app_admin_front_file);
						}
						
						include_once($app_admin_detail_file);
					} elseif( is_file($app_admin_home_file) ) {
						if( file_exists($app_admin_front_file) ) {
							include_once($app_admin_front_file);	
						}
						
						include_once($app_admin_home_file);
					}
				} elseif( $request_link_subclass != '' && is_file($app_detail_file) ) {
					if( file_exists($app_front_file) ) {
						include_once($app_front_file);	
					}
					
					include_once($app_detail_file);
				} elseif( is_file($app_home_file) ) {
					if( file_exists($app_front_file) ) {
						include_once($app_front_file);	
					}					
					
					include_once($app_home_file);
				} else {
					$request_notexist = true;
				}
			} elseif( util_file_exists(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/', $request_link_class, '.app.front.php', 'request_link_class') ) {
				include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/' . $request_link_class . '.app.front.php');
			} else {
				$request_notexist = true;
			}
			//
			if( $request_notexist == true ) {
				include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/' . 'home.app.front.php');
			}
			break;
	}
} else {
	include_once(CONFIG_PATH . CONFIG_DIR_FRONTS . 'app/home.app.front.php');
}
//
?>